<?php


namespace App\Repositories\System;


use App\Models\BaseModel;
use App\Models\Ca\ConsultationWay;
use App\Models\System\Area;
use App\Repositories\BaseRepository;
use App\Traits\RequestCache;
use App\Utils\Output;
use App\Utils\TreeUtil;

class AreaRepository extends BaseRepository
{
    use RequestCache;

    function getName($id)
    {
        return $this->requestCache('name_' . $id, function () use ($id) {
            return Area::query()->where('id', '=', $id)->value('name');
        });
    }

    function load($params)
    {
        extract($params);
        $query = Area::query();
        if (!empty($pid)) {
            $query->where('pid', '=', $pid);
        }
        return Output::data($query->orderByDesc('sort')->get());
    }

    function tree()
    {
        $query = Area::query();
        $query->select('id', 'pid', 'name');
        $lis = $query->get()->toArray();
        return Output::data(TreeUtil::toTree($lis));
    }

    function getModel(): BaseModel
    {
        return new Area();
    }
}